package com.donger.boms.service.Impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donger.boms.entity.Product;
import com.donger.boms.exception.BizException;
import com.donger.boms.mapper.ProductMapper;
import com.donger.boms.service.ProductBomService;
import com.donger.boms.service.ProductService;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private final ProductBomService productBomService;
    private final ProductMapper productMapper;

    @Override
    public IPage<Product> pageList(Page<Product> page, Product product) {
        return productMapper.pageList(page,new LambdaQueryWrapper<Product>()
            .like(StrUtil.isNotEmpty(product.getName()),Product::getName,product.getName())
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upload(MultipartFile file) {
        ImportParams params =new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(2);
        params.setReadRows(1);
        List<Product> list;
        try {
            list= ExcelImportUtil.importExcel(
                    file.getInputStream(),
                    Product.class,params
            );
        }catch (Exception e){
            throw new BizException("服务器异常");
        }
        list.stream().peek(i ->{
            if("是".equals(i.getIseffective())){
                i.setIseffective("1");
            }else{
                i.setIseffective("2");
            }
        }).collect(Collectors.toList());
        String bomNumber = list.get(0).getNumber();
        productBomService.upload(file,bomNumber);
        this.saveBatch(list);
        return true;
    }

    @Override
    public void exportExcel(OutputStream outputStream) {
        Map<String,Object> map= new HashMap<>();
        TemplateExportParams params = new TemplateExportParams("templates/ProductImport.xlsx");;
        Workbook workbook= ExcelExportUtil.exportExcel(params,map);
        try {
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
